//
//  BookContentCell.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-10.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "ListenContentCell.h"
//#import "AccountViewController.h"
#import "DownloadManager.h"
#import "AppDelegate.h"

@implementation ListenContentCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)updateDisplay
{
    indexLabel.text = [NSString stringWithFormat:@"%d",self.chapterIndex+1];
    titleLabel.text = [self.chapterInfo valueForKey:@"sub_name"];
    int filesize = [[self.chapterInfo valueForKey:@"file_size"] intValue];
    sizeLabel.text = [NSString stringWithFormat:@"%.1fM",filesize/1024.00/1024.00];
    if (self.chapterIndex%2 == 0) {
        self.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
    }
    else {
        self.contentView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1];
    }
    
    NSString *downloadImageName = nil;
    if ([[DownloadManager defaultManager] isDownloaded:[self.chapterInfo objectForKey:@"subitem_id"]] ||
        [[DownloadManager defaultManager] isDownloading:[self.chapterInfo objectForKey:@"subitem_id"]]) {
        downloadImageName = @"download_over.png";
        shouldDownload = NO;
    } else {
        downloadImageName = @"download_icon.png";
        shouldDownload = YES;
    }
    [downloadButtonIB setBackgroundImage:[UIImage imageNamed:downloadImageName]
                                forState:UIControlStateNormal];
}

- (IBAction)downloadAction:(id)sender
{
    if (![ActivateManager shareManager].isActivated) {
        //        [[UIApplication rootNavController] popViewControllerAnimated:YES];
        //        [[UIApplication rootViewController] showAccountView];
        ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"您尚未激活"] autorelease];
        [promptView show];
        return;
    }
    
    if (!shouldDownload) {
        NSLog(@"已经下载或正在下载中");
        return;
    }
    
    if (![[DownloadManager defaultManager] isConnectionEnable]) {
        ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"请连接网络后重试"] autorelease];
        [promptView show];
        return;
    }
    else if ([[DownloadManager defaultManager] isWWANEnable]) {
        if (![[DownloadManager defaultManager] WWANDownloadEnable]) {
            UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"使用2G/3G网络" message:@"您可在设置中允许使用2G/3G网络" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"继续", nil] autorelease];
            [alertView show];
        }
        return;
    }
    
    [[DownloadManager defaultManager] downloadBook:self.chapterInfo];
    [downloadButtonIB setBackgroundImage:[UIImage imageNamed:@"download_over.png"]
                                forState:UIControlStateNormal];
    ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"已加入下载列表"] autorelease];
    [promptView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//    if (buttonIndex == 1) {
//        [[DownloadManager defaultManager] downloadBook:self.chapterInfo];
//        [downloadButtonIB setBackgroundImage:[UIImage imageNamed:@"download_over.png"]
//                                    forState:UIControlStateNormal];
//        ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"已加入下载列表"] autorelease];
//        [promptView show];
//    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    
    [UIView animateWithDuration:animated?0.5:0 animations:^{
        if (highlighted) {
            self.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        }
        else {
            if (self.chapterIndex % 2 == 0) {
                self.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
            }
            else {
                self.contentView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1];
            }
        }
    }];
}

- (void)dealloc {
    [_chapterInfo release],_chapterInfo = nil;
    [indexLabel release];
    [titleLabel release];
    [sizeLabel release];
    [downloadButtonIB release];
    [super dealloc];
}
@end
